<?php
/**
 * Video controller
 */
namespace App\Http\Controllers;

use App\Service\VideoService;
use App\Validation\ListValidation;
use App\Validation\DeleteValidation;
use App\Validation\DurationValidation;
use App\Service\MemberPraiseService;
use App\Service\MemberCollectService;

class VideoController extends BaseController
{
	const DISPLAY_TRUE = 1;		//显示标识：是
	const DISPLAY_FALSE = 0;	//显示标识：否
	const COLLECT_TRUE = 1;		//收藏标识：是
	const COLLECT_FALSE = 0;	//收藏标识：否

	/**
	 * [indexAction 列表]
	 * @return [type] [description]
	 */
	public function indexAction()
	{
		$this->getUser(0);

		$raw = $this->validation(new ListValidation, $this->request->getJsonRawBody());
		$raw->city_id = $this->city_id;
		$raw->is_display = self::DISPLAY_TRUE;
		$raw->member_id = property_exists($raw,'member_id') ? $raw->member_id : 0;
		$data  = VideoService::getList($raw);

		//获取点赞和收藏
		$praise_data = [];
		$collect_data = [];
		if ($raw->member_id && $raw->member_id >0 && count($data->toArray()) > 0){
			$groupId = VideoService::getTableId();
			$id_arr = array_map('parent::getIdArray',$data->toArray());
			$praise_data  = MemberPraiseService::jugePraiseArr($raw->member_id, $groupId, $id_arr);
			$collect_data = MemberCollectService::jugeCollectArr($raw->member_id, $groupId, $id_arr);
		}

		$d = [];
		foreach ($data as $key => $value) {
			unset($value->index_by);
			unset($value->reco_by);
			$d[$key] = $value;

			//判断缩略图是否有图且url是否完整
			$d[$key]->thumb = $value->thumb ? $this->getFullUrl($value->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';
			$d[$key]->is_collect = intval($value->is_collect) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
			$d[$key]->sources = $value->sources ? $value->sources : "觅窝";

			if (strtotime($value->create_time) < strtotime(date("Y-m-d", time())) && $value->tags == 'new')
				$value->tags = '';

			$d[$key]->create_time = $this->getShowTime($value->create_time);
			$d[$key]->tags = $value->tags ? $value->tags : "";
			// if($d[$key]->is_self == -1)
			// 	$d[$key]->go_url = $d[$key]->three_url ? $d[$key]->three_url : $d[$key]->go_url;
			
			$d[$key]->is_praise  = in_array($value->id, $praise_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
			$d[$key]->is_collect = in_array($value->id, $collect_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;

			$is_praise = $d[$key]->is_praise ? 1 : 0;
			// $d[$key]->go_url = $this->config->gourl . $this->config->urls->video . $value->id . '/' . $value->groupId . '/' . $is_praise;
			$d[$key]->go_url = $this->config->gourl . $this->config->urls->video . $value->id;
			//第三方房源url
			$d[$key]->three_url = $value->three_url ? $value->three_url : $d[$key]->go_url;
		}

		$count = VideoService::getList($raw, TRUE);
		$assign = [
			'total' => $count,
			'list'  => $d,
		];

		return $this->echoJson($assign);
	}

	/**
	 * [detailAction 详情]
	 * @return [type] [description]
	 */
	public function detailAction()
	{
		$this->getUser(0);

		$id = $this->dispatcher->getParam('id');
		if (!$id)
			return $this->echoJson();
		
		if (isset($this->user)){
			$data = VideoService::getInfo($id, $this->user->member_id);
		}else{
			$data = VideoService::getInfo($id);
		}	
		if ($data){
			//判断缩略图是否有图且url是否完整
			// $data->thumb = $data->thumb ? $this->getFullUrl($data->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';
			$data->go_url = $this->config->gourl . $this->config->urls->video . $data->id;
			$data->three_url = $data->three_url ? $data->three_url : $data->go_url;
			$data->comment = $data->comment > 10000 ? floor($data->comment / 10000) . '万+' : $data->comment;
			$data->visit = $data->visit > 10000 ? floor($data->visit / 10000) . '万+' : $data->visit;
			$data->sources = $data->sources ? $data->sources : "觅窝";
			$data->type = $data->sources;
		}

		return $this->echoJson($data);
	}

	public function durationAction()
	{
		$raw = new \stdClass();
		$raw->video = $_POST['video'];
		$raw->duration = $_POST['duration'];
		file_put_contents('./test.txt', json_encode($raw));
		VideoService::duration($raw);
		return $this->echoJson('ok');
	}

	/**
	 * [dwonloadAction 下载量+1]
	 * @return [type] [description]
	 */
	public function downloadAction()
	{
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = VideoService::download($raw->id);
		return $this->echoJson('ok');
	}

	/**
	 * [shareAction 分享量+1]
	 * @return [type] [description]
	 */
	public function shareAction()
	{
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = VideoService::share($raw->id);
		return $this->echoJson('ok');
	}
}